import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class LoginPanel extends JPanel {

    JButton btn1 = new JButton("登录");
    JButton btn2 = new JButton("注册");

    JTextField  userNameText = new JTextField(25);
    JPasswordField passwordField = new JPasswordField(20);
    JProgressBar progressBar;
    JFrame frame;
    Box vBox = Box.createVerticalBox();

    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame();

        frame.setSize(350,170);
        frame.add(new LoginPanel(frame));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation(new Point(screen.width/2 - 100, screen.height/2 - 100));
        frame.setVisible(true);
    }

    public LoginPanel( JFrame frame) {
        this.frame = frame;
        setSize(100,130);
        setBackground(Color.white);
        Box hBox0 = Box.createHorizontalBox();
        JLabel label = new JLabel("高校电子图书管理系统V1.2");
        label.setSize(100,150);
        hBox0.add(label);

        Box hBox1 = Box.createHorizontalBox();
        hBox1.add(new JLabel("姓名:"));
        hBox1.add(userNameText);

        Box hBox2 = Box.createHorizontalBox();
        hBox2.add(new JLabel("密码:"));
        hBox2.add(passwordField);

        Box hBox3 = Box.createHorizontalBox();
        hBox3.add(btn2);
        hBox3.add(Box.createHorizontalStrut(20));
        hBox3.add(btn1);

        vBox.add(Box.createVerticalStrut(5));
        vBox.add(hBox0);
        vBox.add(Box.createVerticalStrut(5));
        vBox.add(hBox1);
        vBox.add(Box.createVerticalStrut(5));
        vBox.add(hBox2);
        vBox.add(Box.createVerticalStrut(5));
        vBox.add(hBox3);
        vBox.add(Box.createVerticalStrut(5));

         JPanel panel = new JPanel();
        LayoutManager layout = new BoxLayout(panel, BoxLayout.PAGE_AXIS);
        panel.setLayout(layout);
        panel.add(vBox);
        panel.setBackground(Color.white);

        add(panel);
        addActionListener(frame,vBox,panel);
    }
    void addActionListener(JFrame frame,Box vBox, JPanel panel ){
        btn1.addActionListener(e -> {
            if("yyb".equals(userNameText.getText()) && "123".equals(String.valueOf(passwordField.getPassword()))){
                Box hBox4 = Box.createHorizontalBox();
                progressBar = new JProgressBar(0, 100);
                progressBar.setValue(0);
                progressBar.setStringPainted(true);
                hBox4.add(progressBar);
                panel.add(hBox4);
                panel.updateUI();

                new Thread(() -> {
                    for (int i = 0; i <= 100; i += 10) {
                        try {
                            TimeUnit.MILLISECONDS.sleep(100);
                        } catch (InterruptedException ex) {
                            throw new RuntimeException(ex);
                        }
                        int value = i;
                        SwingUtilities.invokeLater(() -> {
                            progressBar.setValue(value);
                        });
                    }

                    frame.dispose();
                    new MainPanel();
                }).start();
            }else {
                JOptionPane.showMessageDialog(null, "账号或密码错误!", "ERROR", JOptionPane. ERROR_MESSAGE);
            }
        });

        btn2.addActionListener(e -> {
        });
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
    }
}
